﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MBSDAL;

namespace MBSBusinessEntities
{
    public class CinemaBE : ICrud
   {

        public CinemaBE()
       {
         mbs = new MBS_Entities();
       }

       public CinemaBE(string CinemaName, string Location, int NumOfScreens)
       {
          
           this.CinemaName = CinemaName;
           this.Location = Location;
           this.NumOfScreens = NumOfScreens;
           
           mbs = new MBS_Entities();
       }


       #region ClassVariables

       private MBS_Entities mbs;

       private int cinemaID;
             
       private string cinemaName;

       private string location;

       private int numOfScreens;

       private bool isDeleted;

       private bool isValid =true;

        public static string ErrorMessage;
       

       private List<ScreenBE> screenList=new List<ScreenBE>();
            
       #endregion


       #region Properties

       public int CinemaID
       {
           get { return cinemaID; }
           
       }

       public bool IsValid
       {
           get { return isValid; }
           set { isValid = value; }
       }

       public string CinemaName
       {
           get { return cinemaName; }
           set { cinemaName = value; }
       }

       public string Location
       {
           get { return location; }
           set { location = value; }
       }

       public int NumOfScreens
       {
           get { return numOfScreens; }
           set { numOfScreens = value; }
       }

       public bool IsDeleted
       {
           get { return isDeleted; }
           set { isDeleted = value; }
       }
       #endregion


       #region Methods


       private bool Validate_Insertion()
       {
           if (this.CinemaName == string.Empty) { isValid = false; return false; }
           if (this.Location == string.Empty) { isValid = false; return false; }
           if (this.NumOfScreens == 0) { isValid = false; return false; }
           if (this.isDeleted == true) { isValid = false; return false; }
           return true;
       }

       public bool Add()
       {

           if (Validate_Insertion())
           {

               Cinema cinema = new Cinema();

               try
               {
                   cinema.CinemaName = this.CinemaName;
                   cinema.Location = this.Location;
                   cinema.NumOfScreens = this.NumOfScreens;
                   cinema.IsDeleted = false;
                   mbs.AddToCinemas(cinema);
                   mbs.SaveChanges();
               }
               catch (System.Data.UpdateException ex)
               {
                   ErrorMessage = ex.Message;
                   //Duplicate Cinemaname exists in the database.
                   return false;
               }

               return true;
           }

           return false;
       }

       
       public bool Delete(int CinemaID)
       {

           Cinema cinema = GetCinemaDALByID(CinemaID);

           if (cinema == null)
           {
               return false;
           }

           if (cinema.IsDeleted == true)
           {
               return false;
           }

           this.cinemaID = CinemaID;
           
          screenList =GetAllScreens();
           
           foreach (ScreenBE screen in screenList)
           {
               screen.Delete(screen.CinemaID,screen.ScreenNo);
           }

           cinema.IsDeleted = true;
       
           mbs.SaveChanges();

           return true;
       }


       private CinemaBE ConvertDALToBusinessEntity(Cinema cinemaDAL)
       {

           CinemaBE cinemabe = new CinemaBE();

           cinemabe.cinemaID = cinemaDAL.CinemaID;
           cinemabe.CinemaName = cinemaDAL.CinemaName;
           cinemabe.Location = cinemaDAL.Location;
           cinemabe.NumOfScreens =Convert.ToInt32(cinemaDAL.NumOfScreens);
           cinemabe.IsDeleted = cinemaDAL.IsDeleted;

           return cinemabe;
       }


       private List<Cinema> GetAllDALCinemas()
       {           
           return mbs.Cinemas.Where(x => x.IsDeleted == false).ToList();
       }
       
       public List<CinemaBE> GetAllCinemas()
       {

           List<Cinema> dalcinemalist = GetAllDALCinemas();
           List<CinemaBE> cinemalist = new List<CinemaBE>();

           foreach (Cinema dalcimema in dalcinemalist)
           {
               CinemaBE cinemabe = ConvertDALToBusinessEntity(dalcimema);
               cinemalist.Add(cinemabe);
           }

           return cinemalist;

       }

       public CinemaBE GetCinemaByID(int CinemaID)
       {
           CinemaBE cinemabe=new CinemaBE();
           Cinema cinema = GetCinemaDALByID(CinemaID);
           cinemabe = ConvertDALToBusinessEntity(cinema);
           return cinemabe;
       }


       private Cinema GetCinemaDALByID(int CinemaID)
       {
           Cinema cinemaDAL = new Cinema();
           try
           {
               cinemaDAL = mbs.Cinemas.First(x => x.CinemaID == CinemaID);
           }
           catch (System.InvalidOperationException ex)
           {
               return null;
           }

           return cinemaDAL;
        
       }

       private Cinema GetCinemaDALByName(string CinemaName)
       {
           Cinema cinemaDAL = new Cinema();
           try
           {
               cinemaDAL = mbs.Cinemas.First(x => x.CinemaName.Equals(CinemaName));
           }
           catch (System.InvalidOperationException ex)
           {
               return null;
           }

           return cinemaDAL;

       }



       public bool Update()
       {

           if (Validate_Insertion())
           {

               try
               {
                   Cinema cinema = GetCinemaDALByID(this.CinemaID);

                   cinema.CinemaName = this.CinemaName;
                   cinema.Location = this.Location;
                   cinema.NumOfScreens = this.NumOfScreens;

                   mbs.SaveChanges();
               }
               catch (System.Data.UpdateException ex)
               {
                   ErrorMessage = ex.Message;
                   //Duplicate Cinemaname exists in the database.
                   return false;
               }


               return true;
           }

           return false;
       }

       public object GetByID(int CinemaID)
       {
           CinemaBE cinemabe = new CinemaBE();
           Cinema cinema = GetCinemaDALByID(CinemaID);
           cinemabe = ConvertDALToBusinessEntity(cinema);
           if (Validate_Retrieval(cinemabe))
           {
               return cinemabe;
           }

           return null;

       }

        public bool Validate_Retrieval(CinemaBE cinemabe)
       {
           if (cinemabe == null) { isValid = false; return false; }
           return true;
       }        

       public int GetIDbyName(string CinemaName)
       {
           CinemaBE cinemabe = new CinemaBE();
           Cinema cinema = GetCinemaDALByName(CinemaName);
           cinemabe = ConvertDALToBusinessEntity(cinema);
           return cinemabe.CinemaID;
       }


       #endregion

       #region Methods-Screen

       public bool AddScreen(int ScreenNo, int NumOfSeats)
       {
           ScreenBE screenbe = new ScreenBE(this.CinemaID, ScreenNo, NumOfSeats);
        
           if (this.IsDeleted == false && this.NumOfScreens > GetAllScreens().Count)
        {
            return screenbe.Add();
        }

        else
        {
            return false;
        }
                                       
       }

       public bool RemoveScreen(int ScreenNo)
       {
           ScreenBE screenbe = new ScreenBE();
           
           screenbe.Delete(this.CinemaID, ScreenNo);

           return true;
       }


       public List<ScreenBE> GetAllScreens(){

           ScreenBE screenbe = new ScreenBE();
           screenList=screenbe.GetAllScreens_Cinema(this.CinemaID);
           return screenList;
       }

       public ScreenBE GetScreenByID(int ScreenNo)
       {
           ScreenBE screenbe = new ScreenBE();
           return screenbe.GetScreenByID(this.CinemaID,ScreenNo); 
       }


       private List<Cinema> GetAllDALCinemasToDisplay()
       {

           List<Cinema> cinemaList = new List<Cinema>();
           DateTime StartQueryDate = DateTime.Now;
           DateTime EndQueryDate = StartQueryDate.AddDays(5);

           List<ShowTime> showTimeList = mbs.ShowTimes.Where(s => s.ShowDateTime >= StartQueryDate && s.ShowDateTime <= EndQueryDate && s.IsDeleted == false).ToList();
           
           foreach (ShowTime st in showTimeList)
           {
               Cinema cinema = st.MovieSchedule.Screen.Cinema;
               if (!cinemaList.Contains(cinema) && !cinema.IsDeleted)
                   cinemaList.Add(cinema);
           }

           return cinemaList;
       }

       public List<CinemaBE> GetAllCinemasToDisplay()
       {

           List<Cinema> dalCinemaList = GetAllDALCinemasToDisplay();
           List<CinemaBE> cinemaList = new List<CinemaBE>();

           foreach (Cinema dalCinema in dalCinemaList)
           {
               CinemaBE cinemaBE = ConvertDALToBusinessEntity(dalCinema);
               cinemaList.Add(cinemaBE);
           }

           return cinemaList;

       }


       #endregion



   }
}
